package de.fmaul.dicheck.assertions;

import org.apache.log4j.Logger;

import de.fmaul.dicheck.model.DataSet;
import de.fmaul.dicheck.model.TestCase;
import de.fmaul.dicheck.model.TestOutcome;
import de.fmaul.dicheck.runner.TestResult;

/**
 * Asserts that the resultset has a certain number of rows.
 * 
 * @author Florian Maul (fmaul@gmx.de)
 * 
 *         Usage of the ResultSet is a design flaw. Dataset should be interated
 *         outside of the assertion. Support for multiple assertions is not
 *         possible or at least inefficient.
 * 
 */
public class RowCountAssertion extends AbstractAssertion {
    private static Logger log = Logger.getLogger(RowCountAssertion.class);
    private int assertRowCount = 0;

    public RowCountAssertion(final String message, final int rowCount) {
        assertRowCount = rowCount;
        setMessage(message);
    }

    @Override
    public void checkDataSet(final TestCase testCase,
            final TestResult testResult, final DataSet dataset) {
        log.debug("Checking assertion for testcase " + testCase.getName());
        if (dataset.getFirstTable().getRows().size() != assertRowCount) {
            fail(testResult);
        }
    }

    /**
     * @return only used by unittest cases.
     */
    public int getAssertRowCount() {
        return assertRowCount;
    }

}
